<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>毕业要求</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<!-- 查询条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">毕业要求</label>
                <div class="layui-input-inline">
                    <input type="text" name="content" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">权值</label>
                <div class="layui-input-inline">
                    <input type="text" name="power" autocomplete="off" class="layui-input">
                </div>
            </div>
            <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span
                    class="layui-icon layui-icon-search"></span>查询
            </button>
            <button type="reset"
                    class="layui-btn layui-btn-warm"><span
                    class="layui-icon layui-icon-refresh-1"></span>重置
            </button>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->

<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="graduationTable" lay-filter="graduationTable"></table>
    <div id="graduationToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span
                class="layui-icon layui-icon-add-1"></span>添加毕业要求
        </button>
    </div>

    <div id="graduationRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span
                class="layui-icon layui-icon-edit"></span>更新
        </button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span
                class="layui-icon layui-icon-delete"></span>删除
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <!--隐藏ID修改用-->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">毕业要求</label>
            <div class="layui-input-block">
                <input type="text" name="content" lay-verify="title" autocomplete="off" placeholder="请输入毕业要求"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权值</label>
            <div class="layui-input-block">
                <input type="text" name="power" lay-verify="title" autocomplete="off" placeholder="请输入课程"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<script type="text/javascript" src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    //表格索引
    var tableIns;

    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 2) {
                    return '至少得2个字符啊';
                }
            }
        });

        //表格加载
        tableIns = table.render({
            elem: '#graduationTable'
            , url: '/graduations/page'
            , toolbar: '#graduationToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '毕业要求数据表'
            , height: 'full-280'
            , page: true
            , method: 'post'
            , cols: [
                [
                    {field: 'id', title: 'ID', align: 'center'}
                    , {field: 'content', title: '毕业要求', align: 'center'}
                    , {field: 'power', title: '权值', align: 'center'}
                    , {fixed: 'right', title: '操作', toolbar: '#graduationRowBar', align: 'center'}
                ]
            ], done: function (res, curr, count) { //处理删除某一页只有一条数据 ->往前翻页
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });
        //条件查询监听
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听工具条的事件
        table.on("toolbar(graduationTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                    break;
            }
        });


        //监听行工具条的事件
        table.on("tool(graduationTable)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'update':
                    openUpdateGraduationLayer(data);
                    break;
                case 'delete':
                    deleteGraduation(data);
                    break;
            }
        });

        //监听行双击事件（单击事件为：row）
        table.on('rowDouble(graduationTable)', function (obj) {
            var data = obj.data;
            openUpdateGraduationLayer(data);
        });

        //弹出层索引
        var layerIndex;
        var url;


        //打开添加的弹出层
        function openAddLayer() {
            layerIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['500px', '300px'],
                maxmin: true,
                shadeClose: true,
                title: '添加毕业要求',
                success: function () {
                    $("#dataFrm")[0].reset();
                    url = "/graduations";
                }
            });
        }


        //打开修改的弹出层
        function openUpdateGraduationLayer(data) {
            layerIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['500px', '300px'],
                maxmin: true,
                shadeClose: true,
                title: '修改毕业要求',
                success: function () {
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm", data);
                    url = "/graduations/update";
                }
            });
        }

        //监听提交  添加/修改
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (res) {
                if (res.code == 0) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(layerIndex);
            });
            return false;
        });

        //删除
        function deleteGraduation(data) {
            layer.confirm('你确定要删除【' + data.id + ':' + data.content + '】这条数据吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    var url = "/graduations/" + data.id;
                    $.ajax({
                        type: "delete",
                        url: url,
                        data: null,
                        dataType: "json",
                        async: false,
                        success: function (res) {
                            if (res.code == 0) {
                                tableIns.reload();
                            }
                            layer.msg(res.msg);
                        }
                    });
                    layer.close(index);
                }
            );
        }


    })
</script>


</body>
</html>